package com.hitqz.robot.biz.controller;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hitqz.robot.api.business.entity.CvdQuestionAnswerEntity;
import com.hitqz.robot.api.business.dto.CvdQuestionAnswerQueryDTO;
import com.hitqz.robot.api.business.dto.CvdQuestionAnswerSaveDTO;
import com.hitqz.robot.api.business.dto.CvdQuestionAnswerUpdateDTO;
import com.hitqz.robot.api.business.vo.CvdQuestionAnswerExcelVO;
import com.hitqz.robot.api.business.vo.CvdQuestionAnswerVO;
import com.hitqz.robot.biz.service.CvdQuestionAnswerService;
import com.hitqz.robot.common.core.util.HitConvertUtils;
import com.hitqz.robot.common.core.util.R;
import com.hitqz.robot.common.log.annotation.SysLog;
import com.pig4cloud.plugin.excel.annotation.RequestExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 语音交互问答库
 *
 * @author kehanjiang
 * @date 2025-09-25 10:33:40
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/cvdQuestionAnswer")
@Tag(description = "cvdQuestionAnswer", name = "语音交互问答库管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class CvdQuestionAnswerController {

    private final CvdQuestionAnswerService cvdQuestionAnswerService;

    /**
     * 分页查询
     *
     * @param page 分页对象
     * @param dto  语音交互问答库
     * @return
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/page")
    public R<Page<CvdQuestionAnswerVO>> getCvdQuestionAnswerPage(@ParameterObject Page page, @ParameterObject CvdQuestionAnswerQueryDTO dto) {
        dto = HitConvertUtils.emptyToNull(dto);
        return R.ok(cvdQuestionAnswerService.getCvdQuestionAnswerPage(page, dto));
    }


    /**
     * 通过id查询语音交互问答库
     *
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/{id}")
    public R<CvdQuestionAnswerVO> getById(@PathVariable("id") Long id) {
        return R.ok(cvdQuestionAnswerService.getCvdQuestionAnswerById(id));
    }

    /**
     * 新增语音交互问答库
     *
     * @param dto 语音交互问答库
     * @return R
     */
    @Operation(summary = "新增语音交互问答库", description = "新增语音交互问答库")
    @SysLog("新增语音交互问答库")
    @PostMapping
    public R<Boolean> save(@RequestBody CvdQuestionAnswerSaveDTO dto) {
        return R.ok(cvdQuestionAnswerService.saveCvdQuestionAnswer(dto));
    }

    /**
     * 修改语音交互问答库
     *
     * @param dto 语音交互问答库
     * @return R
     */
    @Operation(summary = "修改语音交互问答库", description = "修改语音交互问答库")
    @SysLog("修改语音交互问答库")
    @PutMapping
    public R<Boolean> updateById(@RequestBody CvdQuestionAnswerUpdateDTO dto) {
        return R.ok(cvdQuestionAnswerService.updateCvdQuestionAnswerById(dto));
    }

    /**
     * 通过id删除语音交互问答库
     *
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除语音交互问答库", description = "通过id删除语音交互问答库")
    @SysLog("通过id删除语音交互问答库")
    @DeleteMapping
    public R<Boolean> removeById(@RequestBody Long[] ids) {
        try {
            return R.ok(cvdQuestionAnswerService.removeCvdQuestionAnswerBatchByIds(CollUtil.toList(ids)));
        } catch (Exception e) {
            return R.failed(e.getMessage());
        }
    }


    /**
     * 批量导入问答数据
     *
     * @param cover         是否覆盖
     * @param excelVOList
     * @param bindingResult 错误信息列表
     * @return R
     */
    @Operation(summary = "批量导入问答数据", description = "批量导入问答数据")
    @SysLog("批量导入问答数据")
    @PostMapping("/import")
    public R importUser(@RequestParam boolean cover, @RequestExcel List<CvdQuestionAnswerExcelVO> excelVOList, BindingResult bindingResult) {
        log.info("导入数据开始:\n{},\n{}", cover, excelVOList);
        return cvdQuestionAnswerService.importData(cover, excelVOList, bindingResult);
    }

}